<?php 
/**
 * Class which all views should extend
 *
 * @author zonk
 */
class View{

        /**
         * @var string Path to the folder which contains the template files
         */
        private $path = '../templates';

        /**
         * @var string default template to use if no template is set
         */
        private $template = 'default';

        /**
         * @var Array contains key-value pairs which have to be insterted in the template
         */
        private $_ = array();

        /**
         * The permission needed to open this view in the browser
         * @var int
         */
        private $permission;

        /**
         * @var UserModel the currentUser
         */
        private $user;

        /**
         * Return the permission attribute
         * @return int
         */
        public function getPermission(){
                return $this->permission;
        }

        /**
         * Sets the permission attribute
         * @param int $newPerm
         */
        public function setPermission($newPerm) {
                $this->permission = $newPerm;
        }

        /**
         * Function to assign the values to keys
         * @param string $key The key to use
         * @param unknown_type $value The value to assign
         */
        public function assign($key, $value){
                $this->_[$key] = $value;
        }

        /**
         * Function to assign a template to this view
         * @param string $template
         */
        public function setTemplate($template){
                $this->template = $template;
        }

        /**
         * Function to load the template file into this view
         * @return string
         */
        public function loadTemplate(){
                $tpl = $this->template;
                if(!$this->user->hasPermission($this->getPermission())){
                        $tpl = 'Loginpage';
                }
                // check path to and existence of template
                $file = $this->path . '/' . $tpl . 'Template.php';
                if(file_exists($file)){
                        ob_start();
                        include $file;
                        $output = ob_get_contents();
                        ob_end_clean();

                        return $output;
                }
                else{
                        return 'could not find template file.';
                }
        }

        public function __construct($user){
                $this->user = $user;
                // Permission default admin to secure setPermission is called everytime!
                $this->setPermission(Permission::ADMIN);
        }
}
?>